/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */

package com.crm.web.page;

import org.apache.tapestry.IPage;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;
import java.util.Date;
import java.util.ArrayList;

import com.crm.model.Process;
import com.crm.model.*;
import com.crm.service.IProcessService;
import com.crm.service.IUserService;
import com.crm.web.util.Tools;
import com.javaforge.tapestry.spring.annotations.InjectSpring;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public abstract class UserRemind extends BasePage implements PageBeginRenderListener {

    private static Log log = LogFactory.getLog(UserRemind.class);
    private int count;
    private int myCount;

    @InjectSpring("processService")
    public abstract IProcessService getProcessService();

    @InjectSpring("userService")
    public abstract IUserService getUserServ();

    @InjectState("userSession")
    public abstract User getUserSession();

    @InjectPage("ProcessRemind")
    public abstract ProcessRemind getProcessRemindPage();

    @InjectPage("ProcessModify")
    public abstract ProcessModify getProcessModifyPage();

    /**
     * To displayed remind info list of staff according to staff code
     *
     * @param uid The id of user.
     * @return A root level component responsible for generating an entire a page within the application.
     */
    public IPage onShowDetails(String uid) {
        User user = getUserServ().getUserById(uid);
        log.info(getUserSession().getUsername() + 
                ": To list remind according to usercode:" + user.getUserCode());
        ProcessRemind page = getProcessRemindPage();
        page.setPageSelectedUser(user);
        return page;
    }

    /**
     * forward to page ProcessRemind if staffType isn't 0
     *
     * @param event Encapsulates information related to the page listener interfaces.
     */
    public void pageBeginRender(PageEvent event) {
        String userGroup = getUserSession().getUserGroup();
        if (userGroup == null) {
            event.getRequestCycle().sendRedirect("app?page=SysPageError&service=page");
        }
        else if (!userGroup.equals(User.GROUP_SUPER) && !userGroup.equals(User.GROUP_ADMIN)) {
            getProcessRemindPage().setPageSelectedUser(null);
            event.getRequestCycle().sendRedirect("app?page=ProcessRemind&service=page");
        }
    }

    public String getRecordCount() {
        return "共" + count + "条记录";
    }

    public String getMyRecordCount() {
        return "共" + myCount + "条记录";
    }

    /**
     * list user information.
     *
     * @return The user list.
     */
    public List getEntries() {
        try {
            List list = getUserServ().getUsersExceptSomebody(getUserSession());
            count = list.size();
            log.info(getUserSession().getUsername() + ": list system user!");
            return list;
        }
        catch (Exception e) {
            log.error(getUserSession().getUsername() + ": System Error! "+ e.getMessage());
            e.printStackTrace();
            return new ArrayList();
        }
    }

    public IPage handleRemind(com.crm.model.Process process) {
        ProcessModify page = getProcessModifyPage();
        page.setProcess(process);
        page.setSourcePage(this.getPageName());
        return page;
    }

    /**
     * show my remind information list
     *
     * @return One process information list
     */
    public List getMyEntries() {
        try {
            List allNoHandleInfo = getProcessService().getProcessByNoHandle(getUserSession());

            String currentSystemDate = Tools.formatDate(new Date(), Tools.DATEANDTIME);
            currentSystemDate = currentSystemDate.trim();

            List<Process> remindInfoList = new ArrayList<Process>();
            for (Object anAllNoHandleInfo : allNoHandleInfo) {
                Process process = (Process) anAllNoHandleInfo;
                String remindDate = process.getRemindDate().trim();

                Date remindDateObj = Tools.getDateByString(remindDate);
                Date currentDateObj = Tools.getDateByString(currentSystemDate);
                int compareValue = remindDateObj.compareTo(currentDateObj);

                if (compareValue == 0) {
                    process.setIsHandle("今日提醒");
                    remindInfoList.add(process);
                }
                if (compareValue < 0) {
                    long interval = Tools.getDateInterval(currentSystemDate, remindDate);
                    process.setIsHandle("过期" + interval + "天");
                    remindInfoList.add(process);
                }
            }
            myCount = remindInfoList.size();
            return remindInfoList;
        }
        catch (Exception e) {
            log.warn(getUserSession().getUserType()
                    + "The error happened when display remind info list! Message:"
                    + e.getMessage());
            e.printStackTrace();
            return new ArrayList();
        }
    }
}
